package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.RuleEntity;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 转换器
 *
 * @author marcher
 */
public class SentinelNacosConvert {

    /**
     * RuleEntity -----> Rule<String>
     *
     * @param entities  路由实体
     * @return  字符串
     */
    public static String convertToRule(List<? extends RuleEntity> entities){
        return JSON.toJSONString(entities.stream().map(RuleEntity::toRule).collect(Collectors.toList()));
    }

    /**
     * Rule<String> --> RuleEntity
     *
     * @param rules
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> convertToRuleEntity(String rules, Class<T> clazz) {
        return JSON.parseArray(rules, clazz);
    }
}
